package org.yiming.tools.mqtt.client.client.command;

import lombok.RequiredArgsConstructor;
import org.yiming.tools.mqtt.client.ApplicationContext;
import org.yiming.tools.mqtt.client.client.MqttClient;
import org.yiming.tools.mqtt.client.command.Command;

import java.util.Map;

/**
 * @author renwenpeng
 * date  2022/3/25 5:14 下午
 */
@RequiredArgsConstructor
public class ShowMqttClientCommand implements Command {

    private final String[] names = new String[]{"show"};

    private final ApplicationContext applicationContext;

    @Override
    public String[] name() {
        return names;
    }

    @Override
    public String execute(String[] args) {
        Map<String, MqttClient> mqttClientMap = applicationContext.getMqttClientMap();
        StringBuilder stringBuilder = new StringBuilder();
        mqttClientMap.forEach((key, mqttClient) -> stringBuilder
                .append(mqttClient.getName())
                .append(MqttCommandContext.getSingleton().getMqttClient() != null
                        && mqttClient.getName().equalsIgnoreCase(MqttCommandContext.getSingleton().getMqttClient().getName())
                        ? " <-" : "")
                .append("\n\r"));
        System.out.println(stringBuilder.length() == 0 ? "mqttClient is none" : stringBuilder.toString());
        return null;
    }

    @Override
    public String[] helpContent() {
        return new String[]{"show"};
    }
}
